package com.cs.bd.ad.self.request;

import android.content.Context;
import android.os.Looper;
import android.view.View;

import com.cs.bd.ad.manager.AdSdkManager;

/**
 * Created by Administrator on 2017/12/6.
 */

public class AdsRequestPoxy extends BaseAdsRequest implements BaseAdsRequest.OnRequestListener {

    private int mIndex;

    private BaseAdsRequest[] mAdsRequestArrays;

    public AdsRequestPoxy(Context mContext, AdSdkManager.ILoadAdvertDataListener mLoadAdvertDataListener, BaseAdsRequest... mAdsRequestArrays) {
        super(mContext, mLoadAdvertDataListener);
        this.mAdsRequestArrays = mAdsRequestArrays;
        for (BaseAdsRequest request : mAdsRequestArrays) {
            request.setOnRequestListener(this);
        }
    }

    @Override
    public void requestAds() {
        try {
            mAdsRequestArrays[mIndex].requestAds();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getAdsType() {
        return 0;
    }

    public boolean isFinish() {
        return mIndex == mAdsRequestArrays.length;
    }

    @Override
    public void onError(String s) {
        mIndex++;
        if (isFinish()) {
            mIndex = 0;
            if (null != mLoadAdvertDataListener) {
                mLoadAdvertDataListener.onAdFail(-1);
            }
            return;
        }
        requestAds();
    }

    @Override
    public void onFinish() {
        mIndex = 0;
    }
}
